﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class StockUpdaterCreator : BaseUpdaterCreator<StockModel>
    {
        public StockUpdater CreateStockUpdaterInstance()
        {
            StockUpdater stockUpdater = new StockUpdater(new StockFetcher());
            stockUpdater.DataUpdating += FlagAndApproveFlagValid;
            stockUpdater.DataUpdating += ProductionValid;
            return stockUpdater;
        }

        private void ProductionValid(BaseUpdater<StockModel> sender, DataUpdatingEventArgs<StockModel> e)
        {
            if (e.Insert == null)
                return;
            ProductionFetcher productionFetcher = new ProductionFetcher();
            if(productionFetcher.Get(e.Insert.Production)==null)
            {
                e.Cancel = true;
                e.Message = String.Format("产品{0}不存在",e.Insert.Production);
            }
        }
    }
}
